package com.ctp.thread.passive;

import javax.swing.table.DefaultTableModel;

import com.ctp.bean.FormBean;
import com.ctp.bean.MarketBean;
import com.ctp.bean.msg.TraderStatusMsg;
import com.ctp.controller.manager.TraderManager;
import com.ctp.socket.listener.TraderListener;

public class TraderListenThread extends AbstractTraderListenThread implements
		Runnable {

	private DefaultTableModel formModel = null;

	private DefaultTableModel singleModel = null;

	private TraderManager traderManager = TraderManager.instance();

	public TraderListenThread() {
		this.traderListener = new TraderListener();
	}

	public void setFormTableModel(DefaultTableModel formModel) {
		this.formModel = formModel;
	}

	public void setSingleTableModel(DefaultTableModel singleModel) {
		this.singleModel = singleModel;
	}

	public void insertNewForm(FormBean formBean, String relaFormNum) {
		String[] formData = new String[formModel.getColumnCount()];

		formData[0] = formBean.getSeqNum();
		formData[2] = formBean.getContrName();

		formData[3] = formBean.isBuySellInd() ? "多" : "空";
		formData[4] = formBean.isOpenCloseInd() ? "开" : "平";

		formData[5] = Double.toString(formBean.getFormPrice());
		formData[6] = Integer.toString(formBean.getFormVolume());

		formData[10] = TraderStatusMsg.FORM_DELIVERED;

		formModel.addRow(formData);

		if (relaFormNum == null) {
			return;
		}

		FormBean relaForm = traderManager.getFormByNum(relaFormNum);
		if (relaForm != null) {
			relaForm.setRelativeForm(formBean);
			formBean.setRelativeForm(relaForm);
		}
	}

	public void confirmForm(String[] params) {
		FormBean formBean = traderManager.getFormBySeq(params[1]);
		if (formBean != null) {
			formBean.setFormClientInfo(params[2], params[3], params[4]);
			formBean.setFormReturnInfo(params[5], params[6], params[7],
					params[8]);

			traderManager.addFormByNum(formBean);
		}

		int index = findFormBySeq(formModel, formBean, 0);
		if (index < 0) {
			return;
		}

		formModel.setValueAt(formBean.getFormNum(), index, 1);
		formModel.setValueAt(formBean.getFormTime(), index, 9);

		StringBuffer sb = new StringBuffer();
		if (formBean.getFormNum().length() > 0) {
			sb.append(TraderStatusMsg.FORM_CONFIRMED);
		} else {
			sb.append(TraderStatusMsg.FORM_UNCONFIRMED);
		}

		sb.append(";" + formBean.getFormStatusMsg());
		formModel.setValueAt(sb.toString(), index, 10);
	}

	public void addBargain(String[] params) {
		FormBean formBean = traderManager.getFormByNum(params[1].trim());
		if (formBean != null) {
			formBean.updateBargainInfo(Double.parseDouble(params[2]),
					Integer.parseInt(params[3]));
		}

		int index = findFormBySeq(formModel, formBean, 0);
		if (index < 0) {
			return;
		}

		formModel.setValueAt(formBean.getBargainVolume(), index, 7);
		formModel.setValueAt(formBean.getBargainPrice(), index, 8);

		if (formBean.isTotallyBargain()) {
			formModel.setValueAt(TraderStatusMsg.TOTALLY_BAGAINED, index, 10);
			completeForm(formBean);
		} else {
			formModel.setValueAt(TraderStatusMsg.PARTLY_BAGAINED, index, 10);
		}
	}

	public void completeForm(FormBean formBean) {
		MarketBean[] markets = traderManager.getMarkets();
		formBean.setOriginDiffPrice(markets[2].getLastPrice(),
				markets[0].getLastPrice(), markets[1].getLastPrice());

		String[] formData = new String[singleModel.getColumnCount()];

		formData[0] = formBean.getFormNum();
		formData[1] = formBean.getContrName();

		formData[2] = formBean.isBuySellInd() ? "多" : "空";

		formData[3] = Integer.toString(formBean.getBargainVolume());
		formData[4] = Double.toString(formBean.getBargainPrice());
		formData[5] = Double.toString(formBean.getOriginDiffPrice());

		singleModel.addRow(formData);
	}

}
